<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>管理定区/调度排班</title>
    <!-- 导入jquery核心类库 -->
    <script type="text/javascript" src="../../js/jquery-1.8.3.js"></script>
    <!-- 导入easyui类库 -->
    <link rel="stylesheet" type="text/css" href="../../js/easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../../js/easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="../../js/easyui/ext/portal.css">
    <link rel="stylesheet" type="text/css" href="../../css/default.css">
    <script type="text/javascript" src="../../js/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../../js/easyui/ext/jquery.portal.js"></script>
    <script type="text/javascript" src="../../js/easyui/ext/jquery.cookie.js"></script>
    <script src="../../js/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
    <script type="text/javascript">
        $.fn.serializeJson = function () {
            var serializeObj = {};
            var array = this.serializeArray();
            var str = this.serialize();
            $(array).each(function () {
                if (serializeObj[this.name]) {
                    if ($.isArray(serializeObj[this.name])) {
                        serializeObj[this.name].push(this.value);
                    } else {
                        serializeObj[this.name] = [serializeObj[this.name], this.value];
                    }
                } else {
                    serializeObj[this.name] = this.value;
                }
            });
            return serializeObj;
        };

        //工具栏
        var toolbar = [{
            id: 'button-search',
            text: '查询',
            iconCls: 'icon-search',
            handler: function () {
                $('#searchWindow').window('open');
            }
        }, {
            id: 'button-add',
            text: '增加',
            iconCls: 'icon-add',
            handler: function () {
                if($('#fixedAreaForm').form('validate')) {
                    $('#fixedAreaForm').form('clear');
                }
                $('#addWindow').window('open');
            }
        }, {
            id: 'button-edit',
            text: '修改',
            iconCls: 'icon-edit',
            handler: function () {
                var rows = $('#grid').datagrid("getSelections");
                if(rows.length!=1){
                    $.messager.alert("警告","只能选择一行","warning");
                }else{
                    $('#fixedAreaForm').form('load',rows[0]);
                    $('#addWindow').window('open');
                }
            }
        }, {
            id: 'button-delete',
            text: '删除',
            iconCls: 'icon-cancel',
            handler: function () {

            }
        }, {
            id: 'button-association-customer',
            text: '关联客户',
            iconCls: 'icon-sum',
            handler: associationCustomer
        }, {
            id: 'button-association-courier',
            text: '关联快递员',
            iconCls: 'icon-sum',
            handler: function () {
                var rows = $('#grid').datagrid('getSelections');
                if(rows.length!=1){
                    $.messager.alert("警告","只能选择一个","warning");
                }else{

                    $('#courierFixedAreaId').val(rows[0].id);

                    $('#courierWindow').window('open');
                }

            }
        }];

        //弹出关联客户窗口
        function associationCustomer() {

            var rows = $('#grid').datagrid('getSelections');
            if(rows.length != 1){
                $.messager.alert("警告","只能选择一行","warning");
            }else {
                //保存定区id 到隐藏域
                $('#customerFixedAreaId').val(rows[0].id);

                // 显示进度条--增加体验
                $.messager.progress({
                    interval :500 // 每0.5秒加载10%, 5秒加载完
                });

                //填充窗口的数据
                $.post("../../findNoAssociationCustomer", function (data) {
                    //为关联定区的客户填写
//                    alert(data.length);
                    $('#noassociationSelect').empty();

                    $(data).each(function () {
//                        var content="<option value='"+this.id+"'>"+this.username+"("+this.address+")</option>";
                        var content=$("<option value='"+this.id+"'>"+this.username+"("+this.address+")</option>");
//                        alert(content.val());
//                        $('#noassociationSelect').html(content);
                        $('#noassociationSelect').append(content);
                    })

                }, 'json');

                $.post("../../findAssociationCustomer",{"id":rows[0].id},function (data) {

//                    alert(data[0].id);
                    $('#associationSelect').empty();
                    $(data).each(function () {
                         var content=$("<option value='"+this.id+"'>"+this.username+"("+this.address+")</option>");
                         $('#associationSelect').append(content);
                     }) ;

                });
                // 请求结束后，关闭进度条
                $.messager.progress('close');

                $('#customerWindow').window('open');
            }
        }



        // 定义列
        var columns = [[{
            field: 'id',
            title: '编号',
            width: 80,
            align: 'center',
            checkbox: true
        }, {
            field: 'fixedAreaNum',
            title: '定区编号',
            width: 120,
            align: 'center',
            formatter : function(value,row,index){
                return row.id ;
            }
        }, {
            field: 'fixedAreaName',
            title: '定区名称',
            width: 120,
            align: 'center'
        }, {
            field: 'fixedAreaLeader',
            title: '负责人',
            width: 120,
            align: 'center'
        }, {
            field: 'telephone',
            title: '联系电话',
            width: 120,
            align: 'center'
        }, {
            field: 'company',
            title: '所属公司',
            width: 120,
            align: 'center'
        }]];

        $(function () {
            $('#grid').datagrid({
                iconCls: 'icon-forward',
                fit: true,
                border: true,
                rownumbers: true,
                striped: true,
                pageList: [30, 50, 100],
                pagination: true,
                toolbar: toolbar,
                url: "../../fixedArea_pageQuery.action",
//                url: "../../data/fixed_area.json",
                idField: 'id',
                columns: columns
            });

            //点击关联客户的按钮
            $('#associationBtn').click(function () {
                //拿到关联的窗口里面的option
                $('#associationSelect option').attr("selected","selected");
                //提交表单
                $('#customerForm').submit();

                /*
                //异步提交
                var ids = new Array();
                $('#associationSelect option').each(function () {
                    var val=$(this).val();
                    if(val!='' && val !=null){
                        ids.push(val);
                    }
                });

                $.post("../../associationCustomersToFixedAreaAjax",{"customerIds":ids},function (data) {
                    alert(data);
                    if(data.status=='200'){
                        $.messager.alert("成功","关联成功","success");
                    }
                });
                */


            });

            // 为关联快递员按钮，添加click事件
            $("#associationCourierBtn").click(function(){
                // 判断表单是否通过校验
                if($("#courierForm").form('validate')){
                    $("#courierForm").submit();
                }else{
                    $.messager.alert("警告","表单中存在非法数据","warning");
                }
            });

            //点击向右的建，把左边选中的项添加的左边
            $('#toRight').click(function () {
                 $('#associationSelect').append($('#noassociationSelect option:selected'));
            });
            //向左
            $('#toLeft').click(function () {
                 $('#noassociationSelect').append($('#associationSelect option:selected'));
            });
            
            $('#save').click(function () {
                if ($('#fixedAreaForm').form('validate')) {
                    $('#fixedAreaForm').submit();
                }else{
                    $.messager.alert("警告","哥，没填你提交个毛呀~~","warning");
                }

            })
        });
    </script>
</head>

<body class="easyui-layout">
<div region="center" border="false">
    <table id="grid"></table>
</div>

<!-- 添加 修改定区 -->
<div class="easyui-window" title="定区添加修改" closed="true" id="addWindow" collapsible="false" minimizable="false" maximizable="false"
     style="top:20px;left:200px;height: 300px;width: 400px">
    <div style="height:31px;overflow:hidden;" split="false" border="false">
        <div class="datagrid-toolbar">
            <a id="save" icon="icon-save" href="#" class="easyui-linkbutton" plain="true">保存</a>
        </div>
    </div>

    <div style="overflow:auto;padding:5px;" border="false">
        <form id="fixedAreaForm"
              action="../../fixedArea_save.action" method="post">
            <table class="table-edit" width="80%" align="center">
                <tr class="title">
                    <td>定区信息</td>
                </tr>
                <tr>
                    <td>定区编号</td>
                    <td>
                        <input type="text" name="id" class="easyui-validatebox" required="true"/>
                    </td>
                </tr>
                <tr>
                    <td>定区名称</td>
                    <td>
                        <input type="text" name="fixedAreaName" class="easyui-validatebox" required="true"/>
                    </td>
                </tr>
                <tr>
                    <td>负责人</td>
                    <td>
                        <input name="fixedAreaLeader" class="easyui-validatebox" required="true"/>
                    </td>
                </tr>
                <tr>
                    <td>联系电话</td>
                    <td>
                        <input name="telephone" class="easyui-validatebox" required="true"/>
                    </td>
                </tr>
                <tr>
                    <td>所属公司</td>
                    <td>
                        <input name="company" class="easyui-validatebox" required="true"/>
                    </td>
                </tr>
            </table>
        </form>
    </div>
</div>

<!-- 查询定区 -->
<div class="easyui-window" title="查询定区窗口" id="searchWindow" closed="true" collapsible="false" minimizable="false" maximizable="false"
     style="top:20px;left:200px">
    <div style="overflow:auto;padding:5px;" border="false">
        <form id="searchForm">
            <table class="table-edit" width="80%" align="center">
                <tr class="title">
                    <td colspan="2">查询条件</td>
                </tr>
                <tr>
                    <td>定区编码</td>
                    <td>
                        <input type="text" name="id" class="easyui-validatebox" required="true"/>
                    </td>
                </tr>
                <tr>
                    <td>所属单位</td>
                    <td>
                        <input type="text" name="company" class="easyui-validatebox" required="true"/>
                    </td>
                </tr>
                <tr>
                    <td>分区</td>
                    <td>
                        <input type="text" name="subareaName" class="easyui-validatebox" required="true"/>
                    </td>
                </tr>
                <tr>
                    <td colspan="2"><a id="searchBtn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</a>
                    </td>
                </tr>
            </table>
        </form>
    </div>
</div>

<!-- 关联客户窗口 -->
<div class="easyui-window" title="关联客户窗口" id="customerWindow" modal="true"
     collapsible="false" closed="true" minimizable="false" maximizable="false" style="top:20px;left:200px;width: 700px;height: 300px;">
    <div style="overflow:auto;padding:5px;" border="false">
        <form id="customerForm"
              action="../../associationCustomersToFixedArea.action" method="post">
            <table class="table-edit" width="80%" align="center">
                <tr class="title">
                    <td colspan="3">关联客户</td>
                </tr>
                <tr>
                    <td>
                        <!-- 存放定区编号 -->
                        <input type="hidden" name="id" id="customerFixedAreaId" />
                        <select id="noassociationSelect" multiple="multiple" size="10" style="width: 300px;"></select>
                    </td>
                    <td>
                        <input type="button" value="》》" id="toRight">
                        <br/>
                        <input type="button" value="《《" id="toLeft">
                    </td>
                    <td>
                        <select id="associationSelect" name="customerIds" multiple="multiple" size="10" style="width: 300px;"></select>
                    </td>
                </tr>
                <tr>
                    <td colspan="3"><a id="associationBtn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'">关联客户</a> </td>
                </tr>
            </table>
        </form>
    </div>
</div>


<!-- 关联快递员窗口 -->
<div class="easyui-window" title="关联快递员窗口" id="courierWindow" modal="true"
     collapsible="false" closed="true" minimizable="false" maximizable="false" style="top:20px;left:200px;width: 700px;height: 300px;">
    <div style="overflow:auto;padding:5px;" border="false">
        <form id="courierForm"
              action="../../associationCourierToFixedArea.action" method="post">
            <table class="table-edit" width="80%" align="center">
                <tr class="title">
                    <td colspan="2">关联快递员</td>
                </tr>
                <tr>
                    <td>选择快递员</td>
                    <td>
                        <!-- 存放定区编号 -->
                        <input type="hidden" name="id" id="courierFixedAreaId" />
                        <input type="text" name="courierId" class="easyui-combobox" required="true"
                               data-options="url:'../../courier_findnoassociation.action',
										valueField:'id',textField:'name'"/>
                    </td>
                </tr>
                <tr>
                    <td>选择收派时间</td>
                    <td>
                        <input type="text" name="takeTimeId" class="easyui-combobox" required="true"
                               data-options="url:'../../taketime_findAll.action',
									valueField:'id',textField:'name'" />
                    </td>
                </tr>
                <tr>
                    <td colspan="3"><a id="associationCourierBtn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'">关联快递员</a> </td>
                </tr>
            </table>
        </form>
    </div>
</div>



</body>

</html>